package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.Detail;
import com.example.pojo.WorkOrder;
import com.example.mapper.WorkOrderMapper;
import com.example.service.IDetailService;
import com.example.service.ITransferService;
import com.example.service.IWorkOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.DetailVo;
import com.example.vo.TransferVo;
import com.example.vo.WorkOrderDetailVo;
import com.example.vo.WorkOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-02-20
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements IWorkOrderService {
    @Autowired
    private IDetailService detailService;

    @Autowired
    private ITransferService transferService;

    @Override
    public Page<WorkOrderVo> queryByCondition(Page<WorkOrderVo> page, Map<String, Object> map) {
        QueryWrapper<WorkOrderVo> wrapper = new QueryWrapper();
        wrapper.apply(" wo.del_flag=0 ")
                .eq(!ObjectUtils.isEmpty(map.get("status")), "wo.status", map.get("status"))
                .and(!ObjectUtils.isEmpty(map.get("officeId")),
                        qw -> qw.eq("su1.office_id", map.get("officeId")).or()
                                .eq("su2.office_id", map.get("officeId")).or()
                                .eq("su3.office_id", map.get("officeId")));
        baseMapper.queryByCondition(page, wrapper);
        return page;
    }

    /**
     * 根据工单code获取工单详细信息以及关联危废品详情和转运记录
     * @param code
     * @return
     */
    @Override
    public WorkOrderDetailVo queryWorkOrderDetailByWorkOrderCode(String code) {
        //构造查询条件
        QueryWrapper<WorkOrderDetailVo> wrapper = new QueryWrapper<>();
        wrapper.apply(" wo.del_flag=0 ")
                .eq(!ObjectUtils.isEmpty(code),"wo.code",code);
        //获取工单详细信息
        WorkOrderDetailVo workOrderDetailVo = baseMapper.queryWorkOrderByWorkOrderCode(wrapper);
        //获取工单关联危废品详情
        DetailVo detailVo = detailService.queryByWorkOrderId(workOrderDetailVo.getId());
        //获取工单关联转运记录集合
        List<TransferVo> transferVoList = transferService.queryByWorkOrderId(workOrderDetailVo.getId());

        workOrderDetailVo.setDetailVo(detailVo);
        workOrderDetailVo.setTransferVoList(transferVoList);
        return workOrderDetailVo;
    }

    /**
     * 根据detailID获取工单详细信息以及关联危废品详情
     * @param detailId
     * @return
     */
    @Override
    public WorkOrderDetailVo queryWorkOrderDetailByDetailId(String detailId) {
        //获取detail对象
        Detail byId = detailService.getById(detailId);
        System.out.println("detail：" + byId);
        //获取工单对象
        WorkOrder workOrder = this.getById(byId.getWorkOrderId());
        System.out.println("workOrder：" + workOrder);
        //构造查询条件
        QueryWrapper<WorkOrderDetailVo> wrapper = new QueryWrapper<>();
        wrapper.apply(" wo.del_flag=0 ")
                .eq(!ObjectUtils.isEmpty(workOrder.getCode()),"wo.code",workOrder.getCode());

        //获取工单详情记录
        WorkOrderDetailVo workOrderDetailVo = baseMapper.queryWorkOrderByWorkOrderCode(wrapper);
        //获取工单对应detail详情记录
        DetailVo detailVo = detailService.queryByWorkOrderId(workOrderDetailVo.getId());
        workOrderDetailVo.setDetailVo(detailVo);
        return workOrderDetailVo;
    }
}
